package com.shbaiche.bifenba.adapter.match;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.match.MatchesVo;
import com.shbaiche.bifenba.model.match.TeamHistoryVo;
import com.shbaiche.bifenba.ui.match.MatchDetailActivity;
import com.shbaiche.bifenba.view.CircularImage;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroid.kit.KnifeKit;
import cn.droidlover.xdroidbase.imageloader.ILFactory;

/**
 * Created by wanglei on 2016/12/10.
 */

public class MatchFightHistoryListAdapter extends SimpleRecAdapter<TeamHistoryVo, MatchFightHistoryListAdapter.ViewHolder> {
    private Activity context;
    private MatchesVo matchesVo;
    private int color_win , color_flat, color_lose;

    public MatchFightHistoryListAdapter(Activity context,MatchesVo matchesVo) {
        super(context);
        this.context = context;
        this.matchesVo = matchesVo;
        color_win = context.getResources().getColor(R.color.match_win);
        color_flat = context.getResources().getColor(R.color.match_flat);
        color_lose = context.getResources().getColor(R.color.match_lose);
    }

    @Override
    public ViewHolder newViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_match_fight_history_list;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        TeamHistoryVo item = data.get(position);

        holder.tvName.setText(item.getCompeteName());
        holder.tvDatee.setText(item.getMontAndDay()+"\n"+item.getYear());
        holder.tvNameHome.setText(item.getHomeName());
        holder.tvNameVisie.setText(item.getVisitName());
        holder.tvScore.setText(item.getScore());

        int scoreType = 1;
        if(!TextUtils.isEmpty(item.getScore()) && item.getScore().contains("-")){
            String[] scores = item.getScore().split("-");
            try{
                int left = Integer.valueOf(scores[0]);
                int right = Integer.valueOf(scores[1]);

                if(left == right){
                    scoreType = 0;
                }else if(left > right){
                    scoreType = 1;
                }else {
                    scoreType = -1;
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }


        if(item.getHomeName().equals(matchesVo.getHomeName())){
            holder.tvNameHome.setTextColor(color_win);
            holder.tvNameVisie.setTextColor(color_lose);
            switch (scoreType){
                case -1:
                    holder.tvScore.setBackgroundResource(R.drawable.bg_pb_lose);
                    break;
                case 0:
                    holder.tvScore.setBackgroundResource(R.drawable.bg_pb_flat);
                    break;
                case 1:
                    holder.tvScore.setBackgroundResource(R.drawable.bg_pb_win);
                    break;
            }
        }else{
            holder.tvNameHome.setTextColor(color_lose);
            holder.tvNameVisie.setTextColor(color_win);

            switch (scoreType){
                case -1:
                    holder.tvScore.setBackgroundResource(R.drawable.bg_pb_win);
                    break;
                case 0:
                    holder.tvScore.setBackgroundResource(R.drawable.bg_pb_flat);
                    break;
                case 1:
                    holder.tvScore.setBackgroundResource(R.drawable.bg_pb_lose);
                    break;
            }
        }



        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getRecItemClick() != null) {

                }
            }
        });
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {



        @BindView(R.id.tv_name_home)
        TextView tvNameHome;

        @BindView(R.id.tv_name_visit)
        TextView tvNameVisie;

        @BindView(R.id.tv_date)
        TextView tvDatee;

        @BindView(R.id.tv_score)
        TextView tvScore;

        @BindView(R.id.tv_name)
        TextView tvName;




        public ViewHolder(View itemView) {
            super(itemView);
            KnifeKit.bind(this, itemView);
        }
    }
}
